package com.supermarket_project.controller;

import com.supermarket_project.common.R;
import com.supermarket_project.service.LoadingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RestController
//@RequestMapping("/supermarket")
@CrossOrigin//解决跨域
public class LoadingController {

    @Autowired
    private LoadingService loadingService ;

    @RequestMapping(value = "/loading",method = RequestMethod.POST)
    public R LoadingUpdate_c(@RequestBody List<Map> data){
        System.out.println(data.get(0).get("serial_number"));
        return loadingService.insertLoading_s(data);
    }

    @RequestMapping(value = "/querySerialNumber",method = RequestMethod.GET)
    public R OnShowQuerySerialNumber_c(){
        return loadingService.querySerialNumber_s();
    }

    @RequestMapping(value = "/selectByBarcode",method = RequestMethod.POST)
    public R selectByBarcode_c(@RequestBody Map data){
        return loadingService.selectByBarcode_s(data.get("load_goods_num").toString());
    }

    @RequestMapping(value = "/processExcel",method = RequestMethod.POST)
    public R processExcel_c(@RequestParam("excelLoading") MultipartFile excelFile) throws Exception{
        if (excelFile.isEmpty()){
            R r =new R();
            r.setCode(500);
            r.setMsg("Excel文件为空");
            return r;
        }
        String fileName = excelFile.getOriginalFilename();//获取文件名
        InputStream in = excelFile.getInputStream();//获取文件输入流

        return loadingService.processExcel_s(in,fileName);
    }
}
